<?php


namespace App\Services\Game;

use App\DTOS\Admin\ServerGroupDTO;
use App\Models\Game\ConfigGroup;
use App\Models\Game\GroupChannel;
use App\Models\Game\GroupPackage;
use App\Models\Game\ServerGroup;
use Illuminate\Support\Facades\DB;

class ServerGroupService
{
    public function paginate(ServerGroupDTO $DTO){

        $model = ServerGroup::query();
        $pageSize = $DTO->getLimit();
        $page = $DTO->getPage();
        $name = $DTO->getName();
        if($name) {
            $model->where('name','like',"%{$name}%");
        }
        return $model->orderByDesc('id')->with(['channels','packages'])->paginate($pageSize,'*','page',$page);
    }

    public function delete($id){
        ServerGroup::query()->where('id',$id)->delete();
        GroupChannel::query()->where('server_group_id',$id)->delete();
        GroupPackage::query()->where('server_group_id',$id)->delete();
        return true;
    }
    public function edit($groupId,$data){
        $updateData = array_only($data,['name','gameVersionId']);
        $channelIds = data_get($data,'channelIds',[]);
        $packageIds = data_get($data,'packageIds',[]);

        // 处理 configId，确保只有一个值
        $configIds = data_get($data, 'configId', []);
        $configId = !empty($configIds) ? $configIds[0] : null; // 获取第一个元素，或为 null

        // 将 configId 更新到 insertData
        $updateData['type'] = $configId;

        DB::beginTransaction();
        try {
            ServerGroup::query()->where('id',$groupId)->update($updateData);
            $groupChannelList = [];
            $groupPackageList = [];
            foreach ($channelIds as $channelId){
                $groupChannelList[] = [
                    'server_group_id'=>$groupId,
                    'game_channel_id'=>$channelId,
                ];
            }
            foreach ($packageIds as $packageId){
                $groupPackageList[] = [
                    'server_group_id'=>$groupId,
                    'game_package_id'=>$packageId,
                ];
            }

            GroupChannel::query()->where('server_group_id',$groupId)->delete();
            GroupPackage::query()->where('server_group_id',$groupId)->delete();

            GroupChannel::query()->insert($groupChannelList);
            GroupPackage::query()->insert($groupPackageList);

            DB::commit();
        } catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }

    }
    public function add($data){
        $insertData = array_only($data,['name','gameVersionId']);
        $channelIds = data_get($data,'channelIds');
        $packageIds = data_get($data,'packageIds');
        // 处理 configId，确保只有一个值
        $configIds = data_get($data, 'configId', []);
        $configId = !empty($configIds) ? $configIds[0] : null; // 获取第一个元素，或为 null

        // 将 configId 更新到 insertData
        $insertData['type'] = $configId;
        DB::beginTransaction();
        try {
            $groupId = ServerGroup::query()->insertGetId($insertData);
            $groupChannelList = [];
            $groupPackageList = [];
            foreach ($channelIds as $channelId){
                $groupChannelList[] = [
                    'server_group_id'=>$groupId,
                    'game_channel_id'=>$channelId,
                ];
            }
            foreach ($packageIds as $packageId){
                $groupPackageList[] = [
                    'server_group_id'=>$groupId,
                    'game_package_id'=>$packageId,
                ];
            }
            GroupChannel::query()->insert($groupChannelList);
            GroupPackage::query()->insert($groupPackageList);

            DB::commit();
        } catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }
    public function detail($id){

        $detail = ServerGroup::query()->with(['channels'=>function($channel){
            $channel->with('packages');
        },'packages'])->find($id);

        // 查询对应的 ConfigGroup 数据
        $configs = ConfigGroup::query()->where('id', $detail->type)->get();

        // 将 ConfigGroup 数据添加到 detail
        $detail->configs = $configs;
        return $detail;
    }

}
